/*parameters linked to investigator.jsp*/
var investigatorId = ["#piId","#siId"];
var investigatorSelected = ["#searchPI","#searchSI"];
var investigatorShowMoreInfoId = ["#pi-show-hide","#si-show-hide"];
var investigatorMoreInfo = ["#selected-pi-Information","#selected-si-Information"];
var cdaId = "#cda-show-hide";
var cdaInfo = "#selected-cda-Information";

/*investigator constructor*/
function Investigator(id,nombre,apellido,cuit,degree,email,fax,phone){
    var investigator = new Object();
    investigator.id = id;
    investigator.nombre = nombre;
    investigator.apellido = apellido;
    investigator.cuit = cuit;
    investigator.degree = degree;
    investigator.email = email;
    investigator.fax = fax;
    investigator.phone = phone;
    return investigator;
}

function searchInvestigator(nameOrSurname, url){
    windowCaller("Select Investigator", url, { "nameOrSurname": nameOrSurname });
}


/*
* Receives an investigator
* index = 0 => PI
* index = 1 => SI
* */
function closeWindowsInvestigaror(investigator, index){
    $.window.closeAll();

    /*for the label*/
    $(investigatorSelected[index]).val(investigator.nombre+' '+investigator.apellido);

    $(investigatorId[index]).val(investigator.id);

    /*
    Build html more information for site. It has information about its addresses
     */
    builMoreInfoHTMLForInvestigator(investigator,index);
}

/*It prints addresses information*/
function builMoreInfoHTMLForInvestigator(investigator, index){
    $(investigatorMoreInfo[index]).empty();
    var result = '<label style="display:inline; color: #919296;">Cuit:</label> '+investigator.cuit+'<br/>' +
        '<label style="display:inline; color: #919296;">Degree:</label> '+investigator.degree+'<br/>'  +
        '<label style="display:inline; color: #919296;">email:</label> '+investigator.email+'<br/>'  +
        '<label style="display:inline; color: #919296;">Fax:</label> '+investigator.fax+'<br/>'  +
        '<label style="display:inline; color: #919296;">Phone:</label> '+investigator.phone+'<br/>';

    $(investigatorMoreInfo[index]).append(result);
    $(investigatorShowMoreInfoId[index]).show();

    /*En caso de que sea PI*/
    if(index == 0){
        loadCDAsMolecula(investigator);
        loadPIDataForSite(investigator);
    }
}

function loadCDAsMolecula(investigator){
    $(cdaInfo).empty();
    $.ajax({
        type: "POST",
        url: "findByInvolucradoId",
        data: {"investigadorId" : investigator.id },
        success: function(result)
        {
            $(cdaInfo).empty();
            if(result.trim() != ''){
                $(cdaInfo).append(result);
                $(cdaId).show();
            }else{
                $(cdaInfo).append("<span style=\"font-size:1.2em;\">No results found</span>");
                $(cdaId).show();
            }
        }
    });
}

function loadPIDataForSite(investigator){
    var siteId = $('#siteId').val();
    if(siteId.trim() != ''){
        $.ajax({
            type: "POST",
            url: "findIPCDataForPIAndSite",
            async: false,
            data: {"siteId" : siteId,"investigatorId" : investigator.id },
            success: function(result)
            {
                if(result.trim() != ''){
                    $('#forAjax').empty();
                    $('#forAjax').append(result);
                }
            }
        });

        /*data*/
        $('#piPosition').val($('#piPositionAjax').val());
        $('#piLocation').val($('#piLocationAjax').val());
        $('#piPhoneNumber').val($('#piPhoneNumberAjax').val());

    }
}

